package cn.sibu.myapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Observable;
import java.util.Observer;

public class MainActivity extends AppCompatActivity implements Observer{



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //构建Fragment的回退栈
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.container_fragment,MyFragment.newInstance("First Fragment"));
        ft.commit();

        ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.container_fragment,MyFragment.newInstance("Second Fragment"));
        ft.addToBackStack("second");
        ft.commit();

        ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.container_fragment,MyFragment.newInstance("Third Fragment"));
        ft.addToBackStack("third");
        ft.commit();

        ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.container_fragment,MyFragment.newInstance("Fourth Fragment"));
        ft.addToBackStack("fourth");
        ft.commit();

    }

    public void onHomeClick(View view){
        getSupportFragmentManager().popBackStack("second", FragmentManager.POP_BACK_STACK_INCLUSIVE);


    }

    @Override
    public void update(Observable observable, Object data) {
        Log.e("update","update"+((UIAbservable)observable).getData());
    }


    public static class MyFragment extends Fragment{

        private CharSequence mTitle;
        public  UIAbservable uiAbservable;

        public static MyFragment newInstance(String title){
            MyFragment myFragment  = new MyFragment();
            myFragment.setmTitle(title);
            return myFragment;
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            uiAbservable = new UIAbservable();
            TextView textView = new TextView(getActivity());
            textView.setText(mTitle);
            textView.setBackgroundColor(Color.WHITE);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("onLCick","click");
                   uiAbservable.addObserver(new MyObserver());
                   uiAbservable.setData("hahahaha");
                }
            });
            return textView;
        }

        public void setmTitle(String title){
            mTitle = title;
        }

    }

}
